package mainFragment.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mainFragment.adapter.CollegeIndexVolunteerListAdapter;
import mainFragment.bean.VolunteerListBean;
import view.MyPtrRefresh_LoadView;
import view.MyToFourFragmentDialog;
import volunteer.activity.Volunteer;

/**
 * Created by Administrator on 2017/6/12.
 * 三种志愿者的listView
 */
@ContentView(R.layout.college_index_volunteer_list)
public class CollegeIndexVolunteerList extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.college_index_volunteer_list_layout)
    private MyPtrRefresh_LoadView college_index_volunteer_list_layout;
    @ViewInject(R.id.college_index_volunteer_list_lv)
    private ListView college_index_volunteer_list_lv;
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;

    private String name;
    private String type;
    private int page;
    private String college_id;
    private MyHandler myHandler;

    private ArrayList<VolunteerListBean.VolunteerListbean> arrayList;
    private CollegeIndexVolunteerListAdapter collegeIndexVolunteerListAdapter;
    private final int REFRESH = 0;
    private final int LOAD = 1;


    class MyHandler extends Handler {
        WeakReference<CollegeIndexVolunteerList> weakReference;

        public MyHandler(CollegeIndexVolunteerList collegeIndexVolunteerList) {
            weakReference = new WeakReference<>(collegeIndexVolunteerList);
        }

        @Override
        public void handleMessage(Message msg) {
            CollegeIndexVolunteerList collegeIndexVolunteerList = weakReference.get();
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    Gson gson = new Gson();
                    VolunteerListBean volunteerListBean = gson.fromJson((String) msg.obj, VolunteerListBean.class);
                    if (volunteerListBean != null) {
                        String showcompinfo = volunteerListBean.getShowcompinfo();
                        if ("1".equals(showcompinfo)) {
                            //未登陆
                            final MyToFourFragmentDialog dialog = new MyToFourFragmentDialog(collegeIndexVolunteerList, R.style.confirmDialog);
                            dialog.show();
                        } else {
                            List<VolunteerListBean.VolunteerListbean> volunteerList = volunteerListBean.getVolunteerList();
                            if (volunteerList != null) {
                                if(msg.arg1 == REFRESH){
                                    collegeIndexVolunteerList.arrayList.clear();
                                }
                                if (volunteerList.size() > 0) {
                                    for (int i = 0; i < volunteerList.size(); i++) {
                                        collegeIndexVolunteerList.arrayList.add(volunteerList.get(i));
                                    }
                                } else {
                                    if (msg.arg1 == LOAD) {
                                        page--;
                                    }
                                }
                                collegeIndexVolunteerList.collegeIndexVolunteerListAdapter.notifyDataSetChanged();
                                collegeIndexVolunteerList.college_index_volunteer_list_layout.OnRefreshComplete();
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        name = getIntent().getStringExtra("name");
        college_id = getIntent().getStringExtra("college_id");
        myHandler = new MyHandler(this);
        initView();
        getDataFromInterNet(page = 1, REFRESH, true);
    }


    private void initView() {
        page = 1;
        common_title.setText(name);
        common_back.setOnClickListener(this);
        college_index_volunteer_list_layout.setMyPtrHandler(new MyPtrRefresh_LoadView.MyPtrHandler() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getDataFromInterNet(++page, LOAD, false);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getDataFromInterNet(page = 1, REFRESH, false);
            }
        });
        arrayList = new ArrayList<>();
        collegeIndexVolunteerListAdapter = new CollegeIndexVolunteerListAdapter(this, arrayList);
        college_index_volunteer_list_lv.setAdapter(collegeIndexVolunteerListAdapter);
        college_index_volunteer_list_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(CollegeIndexVolunteerList.this, Volunteer.class);
                i.putExtra("type", arrayList.get(position).getType());
                i.putExtra("id", arrayList.get(position).getId());
                startActivity(i);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                this.finish();
                break;
            default:
                break;
        }
    }


    private void getDataFromInterNet(int page, int flag, boolean dialog) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("college_id", college_id);
        hashMap.put("page", String.valueOf(page));
        switch (name) {
            case Constant.VOLUNTEER_TYPE_OFFICIAL_NAME:
                type = Constant.VOLUNTEER_TYPE_OFFICIAL;
                hashMap.put("type", Constant.VOLUNTEER_TYPE_OFFICIAL);
                break;
            case Constant.VOLUNTEER_TYPE_VIP_NAME:
                type = Constant.VOLUNTEER_TYPE_VIP;
                hashMap.put("type", Constant.VOLUNTEER_TYPE_VIP);
                break;
            case Constant.VOLUNTEER_TYPE_NORMAL_NAME:
                type = Constant.VOLUNTEER_TYPE_NORMAL;
                hashMap.put("type", Constant.VOLUNTEER_TYPE_NORMAL);
                break;
            default:
                break;
        }
        new InterNetController(this, Constant.VOLUNTEERS, myHandler, hashMap, flag, dialog);
    }
}
